package com.hr.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hr.model.UserModel;
import com.hr.service.UserService;
/**
 * 用户控制类 
 * @author fazhen.zheng
 *
 */
@Controller
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("/user/userList")
	public ModelAndView userList(){
		List userList = userService.findAll();
		ModelAndView mv = new ModelAndView("user/userList");
		mv.addObject("users", userList);
		return mv;
		
	}
	@RequestMapping("/user/prepareEditUser")
	public ModelAndView prepareEditUser(String userId){
		UserModel user = userService.findByPrimaryKey(userId);
		ModelAndView mv = new ModelAndView("user/prepareEditUser");
		mv.addObject("User", user);
		return mv;
	}
	@RequestMapping("/user/editUser")
	public ModelAndView editUser(UserModel user){
		userService.editUser(user);
		return userList();
	}
	
	/**
	 * 删除员工
	 * @param code
	 * @return
	 */
	@RequestMapping(value="/user/delete.html")
	public ModelAndView delete(String user_id){
		userService.removeUser(user_id);
		return this.userList();
	}
	/**
	 * 查看个人信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user/userInfo.html")
	public ModelAndView userInfo(HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session = request.getSession();
		UserModel user = (UserModel) session.getAttribute("user");
		ModelAndView mv = new ModelAndView("user/userInfo");
		mv.addObject("User", user);
		return mv;
	}
	/**
	 * 查看员工信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user/user.html")
	public ModelAndView user(String userId){
		ModelAndView mv = new ModelAndView("user/userInfo");
		UserModel user = userService.findByPrimaryKey(userId);
		mv.addObject("User", user);
		return mv;
	}
	
	@RequestMapping(value="/user/editPassword.html")
	public void editPassword(){
	}
	/**
	 * 修改密码
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user/updatePassword.html")
	public ModelAndView updatePassword(HttpServletRequest request,
			HttpServletResponse response){
		String oldPassword = (String) request.getParameter("oldPassword");
		String newPassword= (String) request.getParameter("newPassword");
		String newPassword2= (String) request.getParameter("newPassword2");
		
		String msg = userService.updatePassword((UserModel)request.getSession().getAttribute("user"), oldPassword, newPassword, newPassword2);
		ModelAndView mv = new ModelAndView("user/editPassword");
		mv.addObject("msg", msg);
		return mv;
	}
}
